"""
1,获取json数据
2，解析json数据
3,json取值
4，保存图片
"""
import requests
import os

url = 'https://game.gtimg.cn/images/lol/act/img/js/heroList/hero_list.js?ts=2837022'

headers = {
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
}
responses = requests.get(url=url,headers=headers)

heros = responses.json()['hero']

def save(hero_name,name,img_url):
  filename = f'{hero_name}\\'
  if not  os.path.exists(filename):
      os.mkdir(filename)
  img_content = requests.get(url= img_url).content
  with open(filename + name +'.jpg',mode='wb') as f:
      f.write(img_content)
      print(name)


for hero in heros:
    hero_id = hero['heroId']
    hero_url = f'https://game.gtimg.cn/images/lol/act/img/js/hero/{hero_id}.js'
    reponses_1 = requests.get(url=hero_url,headers=headers)
    #先取hero的key，再取hero中的skins的key
    skins = reponses_1.json()['skins']
    for index in skins:
        hero_name = index['heroName']
        title = index['heroTitle']
        img_name = index['name']
        img_url = index['mainImg']
        if img_url:
            save(hero_name,img_name,img_url)
        else:
            img_url = index['chromaImg']
            save(hero_name,img_name,img_url)

